package vip.xiaonuo.biz.modular.fzrole.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.fzrole.param.*;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.biz.modular.fzrole.entity.BizFzRole;
import vip.xiaonuo.biz.modular.fzrole.service.BizFzRoleService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 分销人角色表控制器
 *
 * @author xiao
 *
 */
@Api(tags = "分销人角色管理")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class BizFzRoleController {

    @Resource
    private BizFzRoleService bizFzRoleService;

    /**
     * 获取分销人角色表分页
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("分页获取分销人角色")
    @GetMapping("/biz/fzrole/page")
    public CommonResult<Page<BizFzRole>> page(BizFzRolePageParam bizFzRolePageParam) {
        return CommonResult.data(bizFzRoleService.page(bizFzRolePageParam));
    }

    /**
     * 添加分销人角色表
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加分销人角色")
    @CommonLog("添加分销人角色")
    @PostMapping("/biz/fzrole/add")
    public CommonResult<String> add(@RequestBody @Valid BizFzRoleAddParam bizFzRoleAddParam) {
        bizFzRoleService.add(bizFzRoleAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑分销人角色表
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑分销人角色")
    @CommonLog("编辑分销人角色")
    @PostMapping("/biz/fzrole/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizFzRoleEditParam bizFzRoleEditParam) {
        bizFzRoleService.edit(bizFzRoleEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除分销人角色表
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除分销人角色")
    @CommonLog("删除分销人角色")
    @PostMapping("/biz/fzrole/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<BizFzRoleIdParam> bizFzRoleIdParamList) {
        bizFzRoleService.delete(bizFzRoleIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取分销人角色表详情
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取分销人角色详情")
    @GetMapping("/biz/fzrole/detail")
    public CommonResult<BizFzRole> detail(@Valid BizFzRoleIdParam bizFzRoleIdParam) {
        return CommonResult.data(bizFzRoleService.detail(bizFzRoleIdParam));
    }

    /**
     * 查询下级角色
     *
     * @author xiao
     *
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("查询下级角色")
    @GetMapping("/biz/fzrole/querySubRoles")
    public CommonResult<List<BizFzRole>> querySubRoles(@Valid BizFzRoleValueParam bizFzRoleValueParam) {
        List<BizFzRole> subRoles = bizFzRoleService.querySubRoles(bizFzRoleValueParam.getRoleValue());
        if (subRoles.isEmpty()) {
            return CommonResult.error("该角色不存在下级角色");
        }
        return CommonResult.data(subRoles);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation("生成小程序邀请码")
    @PostMapping("/generateWxaQrCode")
    public CommonResult<String> generateWxaQrCode(@RequestBody WxQrCodeParam wxQrCodeParam) {
        return CommonResult.data(bizFzRoleService.generateMaQrCode(wxQrCodeParam));
    }
}
